package com.yksj.healthtalk.adapter;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.LevelListDrawable;
import android.graphics.drawable.RotateDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yksj.healthtalk.entity.BaseInfoEntity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.app.HTalkApplication;
import com.yksj.healthtalk.ui.salon.SalonInformationActivity;
import com.yksj.healthtalk.ui.salon.SalonReadSelf;
import com.yksj.healthtalk.utils.PersonInfoUtil;

public class ChatLatelyListAdapter extends BaseExpandableListAdapter{
		private final LayoutInflater mInflater;
		private List<String[]> groupList = new ArrayList<String[]>();
		public List<List<BaseInfoEntity>> childList = new ArrayList<List<BaseInfoEntity>>();
		
		private final ImageLoader mImageLoader;
		private final ConcurrentHashMap<String,List<MessageEntity>> mMesgesMap;
		
		public void setonClickSalonAttentionListener(onClickSalonAttentionListener l){
			this.mAttentionListener = l;
		}
		public interface onClickSalonAttentionListener{
			void onSalonAttentionClick(View v,GroupInfoEntity mSalonName);
		}
		onClickSalonAttentionListener mAttentionListener ;
		onClickFriendAttentionListener attentionListener;
		private Context context;
		private boolean mUserIsDoctor;
		
		
		public interface onClickFriendAttentionListener{
			void onClickFriendAttention(int attentioned,CustomerInfoEntity entity);
		}
		
		
		public void setonClickFriendAttentionListener(onClickFriendAttentionListener attentionListener){
			this.attentionListener = attentionListener;
		};
		
		public ChatLatelyListAdapter(LayoutInflater inflater,Context context) {
			this.mImageLoader = ImageLoader.getInstance();
			this.mInflater = inflater;
			this.context=context;
			mUserIsDoctor = SmartFoxClient.getLoginUserInfo().isDoctor();
			mMesgesMap = HTalkApplication.getAppData().messageCllection;
			groupList.add(new String[]{"助理消息","0"});
			groupList.add(new String[]{"朋友消息","0"});
			groupList.add(new String[]{"话题消息","0"});
			groupList.add(new String[]{"医生消息","0"});
			groupList.add(new String[]{"陌生人消息","0"});
			
		}
		/**
		 * 更新数据
		 * @param groupLists
		 * @param childList
		 */
		public void onUpdateData(List<String[]> groupLists,List<List<BaseInfoEntity>> childList){
			this.groupList = groupLists;
			this.childList = childList;
			if(!mUserIsDoctor){
				this.groupList.remove(2);
				this.childList.remove(2);
			}
			
			notifyDataSetInvalidated();
		}

		@Override
		public int getGroupCount() {
			return groupList.size();
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return childList.get(groupPosition).size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return groupList.get(groupPosition);
		}

		@Override
		public BaseInfoEntity getChild(int groupPosition, int childPosition) {
			return childList.get(groupPosition).get(childPosition);
		}

		@Override
		public long getGroupId(int groupPosition) {
			// TODO Auto-generated method stub
			return groupPosition;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			// TODO Auto-generated method stub
			return childPosition;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}
		
		@Override
		public int getChildType(int groupPosition, int childPosition) {
			String string = groupList.get(groupPosition)[0];
			if("话题消息".equals(string)){//群item layout
				return 1;
			}else{
				return 2;
			}
		}
		
		@Override
		public int getChildTypeCount() {
			return 3;
		}
		
		/**绑定数据
		 * @param viewHolder
		 * @param object
		 */
		public void onBindData(final ViewHolder viewHolder,BaseInfoEntity object){
			if(object==null)return;
			if(object instanceof CustomerInfoEntity){
				final CustomerInfoEntity entity = (CustomerInfoEntity)object;
				LevelListDrawable listDrawable = (LevelListDrawable)viewHolder.sexImageV.getDrawable();
				listDrawable.setLevel(Integer.valueOf(entity.getSex()));
				viewHolder.nameTextV.setText(entity.getName());
				viewHolder.noteTextV.setText(entity.getDescription());
				viewHolder.levelImageV.setVisibility(entity.isShowDoctorV() ? View.VISIBLE:View.GONE);
				final List<MessageEntity> list = mMesgesMap.get(entity.getId());
				if(list != null){
					viewHolder.mesgTextV.setVisibility(View.VISIBLE);
					viewHolder.mesgTextV.setText(String.valueOf(list.size()));
				}else{
					viewHolder.mesgTextV.setVisibility(View.GONE);
				}
				mImageLoader.displayImage(entity.getSex(),entity.getNormalHeadIcon(),viewHolder.headerImageV);
				viewHolder.headerImageV.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						PersonInfoUtil.choiceActivity(entity.getId(), context, String.valueOf(entity.getRoldid()));
					}
				});
				
				//点击取消和添加关注安妮
				viewHolder.attention.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (attentionListener != null) {
							attentionListener.onClickFriendAttention(entity.getIsAttentionFriend() ,entity );
						}
					}
				});
				if(entity.getIsAttentionFriend()!=0){
					viewHolder.attention.setImageResource(R.drawable.not_attention);
				}else{
					viewHolder.attention.setImageResource(R.drawable.attention);
				}
				
				//多美助理就不需要关注按钮
				if(SmartFoxClient.helperId.equals(entity.getId())){
					viewHolder.attention.setVisibility(View.GONE);
				}else{
					viewHolder.attention.setVisibility(View.VISIBLE);
				}
				
			}else if(object instanceof GroupInfoEntity){
				final GroupInfoEntity entity = (GroupInfoEntity)object;
				viewHolder.nameTextV.setText(entity.getName());
				viewHolder.noteTextV.setText(entity.getDescription());
				LevelListDrawable levelListDrawable = (LevelListDrawable)viewHolder.levelImageV.getDrawable();
				levelListDrawable.setLevel(Integer.valueOf(entity.getGroupLevel()));
				viewHolder.sexImageV.setVisibility(entity.isSalon() ? View.VISIBLE:View.INVISIBLE);
				viewHolder.monyImageV.setVisibility(entity.isCharge()?View.VISIBLE:View.INVISIBLE);
				final List<MessageEntity> list = mMesgesMap.get(entity.getId());
				if(list != null){
					viewHolder.mesgTextV.setVisibility(View.VISIBLE);
					viewHolder.mesgTextV.setText(String.valueOf(list.size()));
				}else{
					viewHolder.mesgTextV.setVisibility(View.GONE);
				}
				mImageLoader.displayImage("4",entity.getNormalHeadIcon(),viewHolder.headerImageV);
				
				viewHolder.attention.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
							if (mAttentionListener != null) {
								mAttentionListener.onSalonAttentionClick(viewHolder.attention, entity);
							}
						}
					});
				
				//关注状态 1已收藏 
				if (entity.isSalonAttention()) {
					viewHolder.attention.setImageResource(R.drawable.not_attention);
				}else {
					viewHolder.attention.setImageResource(R.drawable.attention);
				}
//				entity.setSalonAttention(true);
//				viewHolder.attention.setImageResource(R.drawable.not_attention);
				
				viewHolder.headerImageV.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						if (entity.getCreateCustomerID().equals(SmartFoxClient.getLoginUserId())) {
							Intent intent = new Intent();
							intent.setClass(context, SalonReadSelf.class);
							intent.putExtra("id", entity.getId());
							context.startActivity(intent);
						} else {
							Intent intent = new Intent();
							intent.setClass(context, SalonInformationActivity.class);
							Bundle bundle = new Bundle();
							intent.putExtra("id", entity.getId());
							intent.putExtras(bundle);
							context.startActivity(intent);
						}
					}
				});
			}
		}
		
		private View createView(int type){
			View convertView;
			final ViewHolder viewHolder = new ViewHolder();;
			if(type == 2){
				convertView = mInflater.inflate(R.layout.friendlist_item1,null);
				convertView.findViewById(R.id.choose).setVisibility(View.GONE);
				viewHolder.headerImageV = (ImageView)convertView.findViewById(R.id.head_image);
				viewHolder.mesgTextV = (TextView)convertView.findViewById(R.id.item_dele);
				viewHolder.nameTextV = (TextView)convertView.findViewById(R.id.name);
				viewHolder.sexImageV = (ImageView)convertView.findViewById(R.id.head_sex);
				viewHolder.levelImageV = (ImageView)convertView.findViewById(R.id.levl);
				viewHolder.attention = (ImageView)convertView.findViewById(R.id.attention);
				viewHolder.noteTextV = (TextView)convertView.findViewById(R.id.note);
			}else{
				convertView = mInflater.inflate(R.layout.grouplist_item1,null);
				viewHolder.headerImageV = (ImageView)convertView.findViewById(R.id.head_image);
				viewHolder.mesgTextV = (TextView)convertView.findViewById(R.id.item_dele);
				viewHolder.nameTextV = (TextView)convertView.findViewById(R.id.name);
				viewHolder.sexImageV = (ImageView)convertView.findViewById(R.id.doctor_salon);
				viewHolder.levelImageV = (ImageView)convertView.findViewById(R.id.levl);
				viewHolder.noteTextV = (TextView)convertView.findViewById(R.id.note);
				viewHolder.monyImageV = (ImageView)convertView.findViewById(R.id.money);
				viewHolder.attention = (ImageView)convertView.findViewById(R.id.attention_salon_iv);
			}
				viewHolder.itemLine = (ImageView)convertView.findViewById(R.id.item_line);
				viewHolder.itemLine.setVisibility(View.VISIBLE);
			convertView.setTag(viewHolder);
			return convertView;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {

			ViewHolder holder;
			if(convertView == null){
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.chat_lately_group_title_layout, null);
				holder.nameTextV = (TextView)convertView.findViewById(R.id.group_name);
				holder.mesgTextV = (TextView)convertView.findViewById(R.id.group_msg_number);
				holder.levelImageV = (ImageView)convertView.findViewById(R.id.group_arrow);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder)convertView.getTag();
			}
			RotateDrawable drawable = (RotateDrawable)holder.levelImageV.getDrawable();
			if(isExpanded){
				drawable.setLevel(10000);
			}else{
				drawable.setLevel(0);
			}
			String name = groupList.get(groupPosition)[0];
			holder.mesgTextV.setText(groupList.get(groupPosition)[1]);
			if(mUserIsDoctor){
				if("病友消息".equals(name))
					holder.nameTextV.setText("朋友消息");
				else
					holder.nameTextV.setText(name);
			}else{
				if("朋友消息".equals(name))
					holder.nameTextV.setText("病友消息");
				else
					holder.nameTextV.setText(name);
			}
			
			return convertView;
		
		}

		@Override
		public View getChildView(int groupPosition, final int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			int type = getChildType(groupPosition, childPosition);
			convertView = createView(type);
			final BaseInfoEntity infoEntity = getChild(groupPosition, childPosition);
			final ViewHolder viewHolder = (ViewHolder)convertView.getTag();
			//之前的需要清除
			if(viewHolder.entity != null){
				viewHolder.entity.mViewHold = null;
			}
			viewHolder.entity = infoEntity;
			viewHolder.entity.mViewHold = new WeakReference(viewHolder);
			onBindData(viewHolder,infoEntity);
			return convertView;
		
		}
		
		public static class ViewHolder{
			BaseInfoEntity entity;
			ImageView headerImageV;
			TextView mesgTextV;
			TextView nameTextV;
			ImageView sexImageV;
			ImageView levelImageV;
			ImageView attention;
			ImageView monyImageV;
			TextView noteTextV;
			ImageView itemLine;
		}
	}